import 'package:appflutterhomeschool/model/questions_model.dart';
import 'package:appflutterhomeschool/res/styles.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../widgets.dart';

///用户： ninetailedfox
///时间： 2020/7/19
///邮箱： 1037438704@qq.com
///功能： 表单组件

//单选
class SingleChoiceWidget extends StatelessWidget {
  final List<Options> optionsList;
  final ValueChanged<int> onTapIndex;

  SingleChoiceWidget({this.optionsList, this.onTapIndex});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(
          left: ScreenUtil().setWidth(34), right: ScreenUtil().setWidth(34)),
      child: ListView.builder(
        physics: NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        itemCount: optionsList.length,
        itemBuilder: (context, index) {
          return Container(
            padding: EdgeInsets.only(bottom: ScreenUtil().setWidth(34)),
            child: GestureDetector(
              onTap: () {
                if (onTapIndex != null) {
                  onTapIndex(index);
                }
              },
              child: Row(
                children: <Widget>[
                  Icon(
                    optionsList[index].select
                        ? Icons.adjust
                        : Icons.panorama_fish_eye,
                    size: ScreenUtil().setSp(60),
                    color:
                        optionsList[index].select ? Colors.blue : Colors.grey,
                  ),
                  Container(
                    child: Text(
                      optionsList[index].option,
                      style: TextStyles.textDark37,
                    ),
                    margin: EdgeInsets.only(left: 5),
                  )
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}

//多选
class MultipleChoiceWidget extends StatelessWidget {
 final  List<Options> optionsList;
  final ValueChanged<int> onTapIndex;

  MultipleChoiceWidget({this.optionsList, this.onTapIndex});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(
          left: ScreenUtil().setWidth(34), right: ScreenUtil().setWidth(34)),
      child: ListView.builder(
        physics: NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        itemCount: optionsList.length,
        itemBuilder: (context, index) {
          return GestureDetector(
            onTap: () {
              if (onTapIndex != null) {
                onTapIndex(index);
              }
            },
            child: Container(
              color: Colors.white,
              padding: EdgeInsets.only(bottom: ScreenUtil().setWidth(34)),
              child: Row(
                children: <Widget>[
                  Icon(
                    optionsList[index].select
                        ? Icons.check_box
                        : Icons.check_box_outline_blank,
                    size: ScreenUtil().setSp(60),
                    color:
                        optionsList[index].select ? Colors.blue : Colors.grey,
                  ),
                  Padding(
                    padding: EdgeInsets.only(left: ScreenUtil().setWidth(34)),
                    child: Text(
                      optionsList[index].option,
                      style: TextStyles.textDark37,
                    ),
                  )
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}

//文本
class FormTextWidget extends StatelessWidget {
  //是否能输入
  final bool enabled;
  final TextEditingController textEditingController;

  FormTextWidget({this.enabled, this.textEditingController});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(
          left: ScreenUtil().setWidth(34), right: ScreenUtil().setWidth(34)),
      child: Theme(
        data: ThemeData(primaryColor: Colors.blue, hintColor: Colors.grey),
        child: TextField(
          controller: textEditingController,
          // 跟最大长度限制对应，如果此属性设置为false,当输入超过最大长度
          // 限制时，依然会显示输入的内容，为true不会（默认为 true）
          maxLengthEnforced: false,
          //是否禁用textfield:如果为false, textfield将被“禁用”
          enabled: enabled ?? false,
          //是否自动更正
          autocorrect: true,
          //光标圆角弧度
          cursorRadius: Radius.circular(5.0),
          maxLines: 5,
          style: TextStyles.textDark43,
          cursorColor: Colors.black,
          decoration: InputDecoration(
            border: OutlineInputBorder(),
            hintText: '',
            counterText: "", //此处控制最大字符是否显示
            hintStyle: TextStyles.textGray37,
          ),
        ),
      ),
    );
  }
}

//相册
class SelectPictureWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        //列表数据
        ListViewWidget(
          type: 0,
          listImage: [],
          onTapIndex: (index) {},
        ),
        Container(
          margin: EdgeInsets.fromLTRB(15, 0, 15, 15),
          padding: EdgeInsets.all(10),
          alignment: Alignment.center,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(5)),
            border: Border.all(color: Colors.grey, width: 0.5),
          ),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Icon(Icons.backup,
                  color: Colors.grey, size: ScreenUtil().setSp(60)),
              SizedBox(width: 5),
              Text("选择图片", style: TextStyles.textGray37)
            ],
          ),
        )
      ],
    );
  }
}

//提示标题
class TitleMenuWidget extends StatelessWidget {
  final String titleContext;
  final QuestionsModel questionsModel;
  final ValueChanged<String> onSelected;
  final bool visibleDelete;

  TitleMenuWidget(
      {this.titleContext,
      this.questionsModel,
      this.onSelected,
      this.visibleDelete});

  @override
  Widget build(BuildContext context) {
    String title;
    bool visible = false;
    if (questionsModel != null) {
      visible = questionsModel.isAnswer == "T";
      title = questionsModel.body;
    }

    return Container(
      margin: EdgeInsets.only(left: 10, right: 10),
      child: Row(
        children: <Widget>[
          Expanded(
              child: Text.rich(TextSpan(children: [
            TextSpan(
                text: title ?? titleContext ?? "未设置标题",
                style: TextStyles.textDark37),
            WidgetSpan(
                child: Visibility(
//                （是：T；否:F）
              visible: visible,
              child: Text("*", style: TextStyles.textRed34),
            ))
          ]))),
          Visibility(
            visible: visibleDelete ?? true,
            child: PopupMenuButton<String>(
                icon: Icon(
                  Icons.more_horiz,
                  size: ScreenUtil().setSp(56),
                  color: Colors.black,
                ),
                //这是点击弹出菜单的操作，点击对应菜单后，改变屏幕中间文本状态，将点击的菜单值赋予屏幕中间文本
                onSelected: onSelected,
                //这是弹出菜单的建立，包含了两个子项，分别是增加和删除以及他们对应的值
                itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
                      PopupMenuItem(
                        child: Text(
                          '编辑',
                          style: TextStyles.textDark37,
                        ),
                        value: '编辑',
                      ),
                      PopupMenuItem(
                        child: Text(
                          '删除',
                          style: TextStyles.textDark37,
                        ),
                        value: '删除',
                      )
                    ]),
          ),
        ],
      ),
    );
  }
}
